package com.heima.wemedia.rabbitlistener;

import com.heima.wemedia.service.WmNewsAutoScanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import java.util.Map;

import static com.heima.wemedia.constants.MqConstants.HMALL_DL_QUEUE;

/**
 * @author Psycho
 * @create 2022-04-27-21:42
 */
@Slf4j
@Component
public class RabbitMqListener {

    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;

    /**
     * 死信队列消费者
     */
    @Transactional(rollbackFor = Exception.class)
    @RabbitListener(queues = {HMALL_DL_QUEUE})
    public void dlQueueConsumer(Integer id) {
        log.info("死信队列消费者 消费消息：{}", id);
        wmNewsAutoScanService.autoScanWmNews( id);
    }

//    /**
//     * 延时队列消费者
//     */
//    //在方法上一次性进行队列声明、交换机声明、绑定关系声明
//    @Transactional(rollbackFor = Exception.class)
//    @RabbitListener(queues = {HMALL_DELAY_QUEUE})
//    public void delayQueueConsumer(Map<String,Object> message) {
//        log.info("延时队列消费者 消费消息：{}", message);
//        Long userId = (Long) message.get("userId");
//        Long orderId = (Long) message.get("orderId");
//        //1、查询订单
//        Order order = orderService.getById(orderId);
//        //2、状态是已支付，无需任何操作
//        if(Objects.isNull(order) || order.getStatus()!=1){
//            return;
//        }
//        //3、状态是未支付，取消订单、回加库存
//        orderService.lambdaUpdate().set(Order::getStatus,5).eq(Order::getId,orderId).update();;
//        List<OrderDetail> orderDetails = orderDetailService.lambdaQuery().eq(OrderDetail::getOrderId, orderId).list();
//        for(OrderDetail orderDetail:orderDetails){
//            itemClient.deduction(orderDetail.getItemId(),orderDetail.getNum());
//        }
//    }
}
